<?php

namespace component\system\system_user;

use common\Common;
use common\model\Model;

/**
 * Class SystemUserModel
 * @package component\system\system_user
 * @property integer $id
 * @property string $username
 * @property string $password
 * @property string $nickname
 * @property string $avatar
 * @property string $phone
 * @property string $email
 * @property integer $status
 * @property string $auth
 * @property integer $create_time
 * @property integer $update_time
 * @property integer $delete_time
 */
class SystemUserModel extends Model
{
    protected $table = 'system_user';

    protected $schema = [
        'id' => 'integer',
        'username' => 'string',
        'password' => 'string',
        'nickname' => 'string',
        'avatar' => 'string',
        'phone' => 'string',
        'email' => 'string',
        'status' => 'integer',
        'create_time' => 'integer',
        'update_time' => 'integer',
        'delete_time' => 'integer',
    ];

    /**
     * @param self $params
     * @return mixed|void
     */
    public static function onBeforeInsert($params)
    {
        if (!$params->password) {
            $params->password = '123456';
        }
    }

    /**
     * @param $value
     * @return string
     */
    protected function setPasswordAttr($value)
    {
        return Common::encryptionPassword($value);
    }

    /**
     * @param $value
     * @return string
     */
    protected function setRoleAttr($value)
    {
        if (is_array($value) && $value) {
            $value = implode(',', $value);
        } else {
            $value = '';
        }

        return $value;
    }

    /**
     * @param $value
     * @return string
     */
    protected function getRoleAttr($value)
    {
        if ($value) {
            $value = explode(',', $value);
        } else {
            $value = '';
        }

        return $value;
    }

    /**
     * 设置头像
     * @param $value
     * @return mixed|string
     */
    protected function setAvatarAttr($value)
    {
        if (is_array($value)) {
            return $value[0] ?? '';
        }

        return $value;
    }
}    